﻿#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
	const float PI = 3.1415926;

	int width = 800;
	int height = 600;
	initgraph(width, height);
	setbkcolor(WHITE);
	setlinestyle(PS_SOLID, 3);

	float line_length = 160;
	float x;
	float y;
	float rotate_speed = PI / 360;
	int line_num = 0;
	float angles[1000];
	int score = 0;
	int i;

	BeginBatchDraw();

	while (1)
	{
		cleardevice();
		setlinecolor(BLACK);
		line(0, height / 2, line_length, height / 2);

		if (_kbhit() && rotate_speed != 0)
		{
			char input = _getch();

			if (input == ' ')
			{
				line_num++;
				angles[line_num - 1] = PI;
				x = line_length * cos(-angles[line_num - 1]) + width / 2;
				y = line_length * sin(-angles[line_num - 1]) + height / 2;
				line(width / 2, height / 2, x, y);

				for (i = 0; i < line_num - 1; i++)
				{
					if (abs(angles[line_num - 1] - angles[i]) < PI / 60)
					{
						rotate_speed = 0;
						break;
					}
				}
				score += 1;
			}
		}

		for (i = 0; i < line_num; i++)
		{
			angles[i] += rotate_speed;

			if (angles[i] > 2 * PI)
			{
				angles[i] -= 2 * PI;
			}
			
			x = line_length * cos(-angles[i]) + width / 2;
			y = line_length * sin(-angles[i]) + height / 2;
			setlinecolor(BLUE);

			if (i == line_num - 1)
			{
				setlinecolor(RED);
			}

			line(width / 2, height / 2, x, y);
		}

		setlinecolor(HSVtoRGB(0, line_num / 60.0, 0.8));
		setlinecolor(HSVtoRGB(0, 0.9, 0.8));
		fillcircle(width / 2, height / 2, 60);
		TCHAR s[20];
		swprintf_s(s, _T("%d"), score);
		settextstyle(50, 0, _T("Times"));
		settextcolor(BLACK);
		outtextxy(65, 200, s);

		FlushBatchDraw();
		Sleep(10);
	}
	closegraph();
	return 0;
}